#!/bin/bash
# Author: Redbrother
# Email: 728083668@qq.com
# WebSite: https://help.ubuntu.com/community/LiveCDCustomizationFromScratch
# license: None
set -e
#自定义版本项
export ARCH=amd64
export RELEASE=jammy
export installer_url=https://mirrors.ustc.edu.cn/ubuntu/
export IMAGE=ubuntu-$RELEASE
export CURDIR=$(cd `dirname $0`;pwd)

if [ "`whoami`" != "root" ]; then
	echo "User must be root 需要root权限."
	exit 0
fi

apt install debootstrap xorriso genisoimage squashfs-tools isolinux syslinux syslinux-efi grub-pc-bin grub-efi-amd64-bin grub-efi-ia32-bin mtools dosfstools -y
if [ "$?" != "0" ]; then
	echo "Commands required: mkisofs, squashfs-tools ,wget."
	exit 0
fi

if [ "`which mkisofs`" == "" ]; then
	ln -s /usr/bin/genisoimage /usr/bin/mkisofs
fi

############################################

chroot squashfs-root apt clean

# make iso file ; build an iso image

mkdir -p cdimage/casper
mkdir -p cdimage/boot/grub
rm -f cdimage/casper/*
##Generate an EFI bootable GRUB image

<<'COMMENT'
cp -rf squashfs-root/usr/lib/grub/i386-pc cdimage/boot/grub/
cp -rf squashfs-root/usr/lib/grub/x86_64-efi cdimage/boot/grub/
grub-mkstandalone -O i386-efi --modules="part_gpt part_msdos fat iso9660" --locales=""  --themes=""  --fonts="" --output="$CURDIR/cdimage/EFI/BOOT/BOOTIA32.EFI" "boot/grub/grub.cfg=$CURDIR/cdimage/boot/grub-embed.cfg"
grub-mkstandalone -O x86_64-efi --modules="part_gpt part_msdos fat iso9660" --locales="" --themes="" --fonts="" --output="$CURDIR/cdimage/EFI/BOOT/BOOTx64.EFI" "boot/grub/grub.cfg=$CURDIR/cdimage/boot/grub-embed.cfg"

( cd "$CURDIR"/cdimage/boot && \
    dd if=/dev/zero of=efiboot.img bs=1M count=8 && \
    sudo mkfs.vfat efiboot.img && \
    sudo mmd -i efiboot.img ::/EFI ::/EFI/BOOT && \
    sudo mcopy -vi efiboot.img \
        "$CURDIR/cdimage/EFI/BOOT/BOOTIA32.EFI" \
        "$CURDIR/cdimage/EFI/BOOT/BOOTx64.EFI" \
        "$CURDIR/cdimage/EFI/BOOT/grub.cfg" \
        ::/EFI/BOOT/
)
COMMENT

<<'COMMENT'
cd "$CURDIR/cdimage/boot/" 
dd if=/dev/zero of=efiboot.img bs=1M count=8
sudo mkfs.vfat efiboot.img
mmd -i efiboot.img ::/EFI ::/EFI/BOOT
mcopy -vi efiboot.img  "$CURDIR/cdimage/EFI/BOOT/BOOTIA32.EFI" "$CURDIR/cdimage/EFI/BOOT/BOOTx64.EFI" "$CURDIR/cdimage/boot/grub/grub.cfg"  ::/EFI/BOOT/
cd ../../
COMMENT

cp -f squashfs-root/boot/vmlinuz-*-generic cdimage/casper/vmlinuz
cp -f squashfs-root/boot/initrd.img-*-generic cdimage/casper/initrd
chroot squashfs-root dpkg-query -W --showformat='${Package} ${Version}\n' | tee cdimage/casper/filesystem.manifest
mksquashfs squashfs-root cdimage/casper/filesystem.squashfs -e boot
chmod +664 -R cdimage/casper
mkdir -p cdimage/.disk
touch cdimage/ubuntu
touch cdimage/.disk/base_installable
echo "full_cd/single"        > cdimage/.disk/cd_type
echo "Ubuntu Remix $RELEASE" > cdimage/.disk/info
cd cdimage && find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt && cd ..

function do_mkiso()
{
### mkiso
cd "$CURDIR"
xorriso \
    -as mkisofs \
    -iso-level 3 \
    -o "$CURDIR/ubuntu-live-custom.iso" \
    -full-iso9660-filenames \
    -volid "DEBLIVE" \
    --mbr-force-bootable -partition_offset 16 \
    -joliet -joliet-long -rational-rock \
    -isohybrid-mbr /usr/lib/ISOLINUX/isohdpfx.bin \
    -eltorito-boot \
        isolinux/isolinux.bin \
        -no-emul-boot \
        -boot-load-size 4 \
        -boot-info-table \
        --eltorito-catalog isolinux/boot.cat \
    -eltorito-alt-boot \
        -e --interval:appended_partition_2:all:: \
        -no-emul-boot \
        -isohybrid-gpt-basdat \
    -append_partition 2 C12A7328-F81F-11D2-BA4B-00A0C93EC93B $CURDIR/cdimage/boot/efiboot.img \
    "$CURDIR/cdimage"
}

do_mkiso
##mkisofs -o $IMAGE.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -r -V "$IMAGE" -cache-inodes -J -l cdimage/
#chmod  +666 $IMAGE.iso
#cd cdimage && rm -f ubuntu md5sum.txt  && cd ..
#rm -rf cdimage/.disk
#rm -rf cdimage/casper/*
#rm -rf cdimage/boot/grub/i386-pc/
#rm -rf cdimage/boot/grub/x86_64-efi/
#rm -rf cdimage/boot/efiboot.img
#echo -e "successfully done"
